//
//  ZSDatePickerView.m
//  VideoExam
//
//  Created by apple on 2020/4/8.
//

#import "ZSDatePickerView.h"
#import "BPDateFormatter.h"
@interface ZSDatePickerView ()
@property (nonatomic,strong) UIDatePicker *pickerView;
@property(nonatomic,strong)NSString * currentDateString;
@property(nonatomic,strong)NSDate * currentDate;
@end
@implementation ZSDatePickerView
- (instancetype)initWithFrame:(CGRect)frame  {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI {
    [self configureView];
    [self addTopBarView];
    [self addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(250);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
}
- (UIDatePicker *)pickerView {
    if (_pickerView==nil) {
        _pickerView=[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
        _pickerView.backgroundColor=APPGrayColor;
    }
    return _pickerView;
}
- (void)configureView {
    // 1.1选择datePickr的显示风格
    [self.pickerView setDatePickerMode:UIDatePickerModeDate];
    
    // 1.2查询所有可用的地区
    //DDLog(@"%@", [NSLocale availableLocaleIdentifiers]);
    // 1.3设置datePickr的地区语言, zh_Han后面是s的就为简体中文,zh_Han后面是t的就为繁体中文
    [self.pickerView setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"]];
    
    // 1.4监听datePickr的数值变化
    [self.pickerView addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    
    // 2.设置日期控件的初始值
    // 2.1 指定一个字符串
//    NSString *dateString = @"2000/10/1";
//
//    // 2.2 把字符串转换成日期
//    NSDateFormatter *fromatter = [[NSDateFormatter alloc] init];
//
//    [fromatter setDateFormat:@"yyyy/MM/dd"];
//    NSDate *date = [fromatter dateFromString:dateString];
    
    // 2.3 将转换后的日期设置给日期选择控件
    [self.pickerView setDate:[NSDate date]];
    [self.pickerView setMinimumDate:[BPDateFormatter dateFromString:@"1918/01/01" fmtStyle:BPDateFormatterStyle_19]];
    self.pickerView.backgroundColor=[UIColor whiteColor];
    [self.pickerView setMaximumDate:[NSDate dateWithTimeIntervalSinceNow:60]];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // 1.1 先设置日期的格式字符串
    [formatter setDateFormat:@"yyyy.MM.dd"];
    
    // 1.2 使用格式字符串, 将日期转换成字符串
    NSString *dateString = [formatter stringFromDate:self.pickerView.date];
    
    self.currentDateString = dateString;
    self.currentDate = self.pickerView.date;
}
- (void)addTopBarView {
    UIView * topView = [[UIView alloc] initWithFrame:CGRectZero];
    topView.backgroundColor=color(@"#FAFAFA");
    topView.layer.cornerRadius = 10;
    topView.layer.qmui_maskedCorners = QMUILayerMinXMinYCorner | QMUILayerMaxXMinYCorner;
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom).offset(-250);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    UIView * line = [[UIView alloc] initWithFrame:CGRectZero];
       line.backgroundColor = color(@"#F0F0F0");
       [topView addSubview:line];
       [line mas_makeConstraints:^(MASConstraintMaker *make) {
           make.bottom.mas_equalTo(topView.mas_bottom);
           make.left.mas_equalTo(0);
           make.right.mas_equalTo(topView.mas_right);
           make.height.mas_equalTo(0.5);
       }];
    UIButton * btn = [UIFactory create_btnWithTitle:@"取消" imageName:nil backgroundColor:nil TypefaceColor:RGBA(0, 0, 0, 0.45) fontSize:16 target:self action:@selector(btnClcicked:)];
    [topView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(2);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    UIButton * btn2 = [UIFactory create_btnWithTitle:@"确定" imageName:nil backgroundColor:nil TypefaceColor:theme_color fontSize:16 target:self action:@selector(btnClcicked:)];
       [topView addSubview:btn2];
       [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
           make.right.mas_equalTo(topView.mas_right).offset(-15);
           make.top.mas_equalTo(2);
           make.size.mas_equalTo(CGSizeMake(40, 40));
       }];
    
}

- (void)dateChanged:(UIDatePicker *)datePicker {
    // 1.要转换日期格式, 必须得用到NSDateFormatter, 专门用来转换日期格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 1.1 先设置日期的格式字符串
    [formatter setDateFormat:@"yyyy.MM.dd"];
    // 1.2 使用格式字符串, 将日期转换成字符串
    NSString *dateString = [formatter stringFromDate:datePicker.date];
    self.currentDateString = dateString;
    self.currentDate = datePicker.date;
}

- (void)btnClcicked:(UIButton *)btn {
    self.backgroundColor=[UIColor clearColor];
    if ([btn.titleLabel.text isEqualToString:@"取消"]) {
        if (self.tableView) {
            [self.tableView reloadData];
        }
      [UIView animateWithDuration:0.5 animations:^{
                  self.top=SCREEN_HEIGHT;
            } completion:^(BOOL finished) {
                 [self removeFromSuperview];
            }];
    } else {
        if (self.completeBlock) {
            self.completeBlock(self.currentDateString);
        }
        [UIView animateWithDuration:0.5 animations:^{
                    self.top=SCREEN_HEIGHT;
            } completion:^(BOOL finished) {
                   [self removeFromSuperview];
        }];
    }
}
+ (void)show_tableView:(UITableView *)tableView CompleteBlock:(void(^)(NSString *selectDateStr))completeBlock  {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
       ZSDatePickerView * selfView = [[ZSDatePickerView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT)];
       selfView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
       [window addSubview:selfView ];
       selfView.tableView=tableView;
       selfView.completeBlock = completeBlock;
}
+ (void)showCompleteBlock:(void(^)(NSString *selectDateStr))completeBlock {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    ZSDatePickerView * selfView = [[ZSDatePickerView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    selfView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
    [window addSubview:selfView ];

    selfView.completeBlock = completeBlock;
}
@end
